const regeneratorRuntime = require('../vendor/regenerator-runtime.js')
const co = require('../vendor/co.js')
let baseUrl = "https://xcx.zhzhsq.com/v1/";//请求根地址
function doRequest(config) {
    return co(function* () {
        //sid
        let sid = wx.getStorageSync('sid')
        if (!sid) {
            //wx login
            let loginRes = yield getApp().toPromise(wx.login)
            let userInfo
            try {
                userInfo = yield getApp().toPromise(wx.getUserInfo)
            } catch (err) {
                return Promise.reject({ code: -1, message: "" })
            }
            console.log('-----')
            let loginInfo = yield getApp().toPromise(wx.request, {
                url: baseUrl + 'login/index',
                header: { 'Code': loginRes.code, 'Encrypted-Data': userInfo.encryptedData, 'IV': userInfo.iv, 'store-id': getApp().storeId }
            })
            if (loginInfo.data.status.code == 2000) {
                //login success
                wx.setStorage({
                    key: 'sid',
                    data: loginInfo.data.data.sid
                })
                sid = loginInfo.data.data.sid
                wx.setStorage({
                    key: 'mid',
                    data: loginInfo.data.data.member_id
                })
                wx.setStorage({
                    key: 'store',
                    data: loginInfo.data.data.store_name
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: '登录失败，请删除小程序后重试',
                })
                return Promise.reject('登录失败')
            }
        }
        let cfg = Object.assign({}, config)
        if (!cfg.header) {
            cfg.header = {}
        }
        cfg.header['store-id'] = getApp().storeId
        cfg.header['sid'] = sid
        let res = yield getApp().toPromise(wx.request, cfg)
        console.log(res)
        let responseCode = res.data.status.code
        if (responseCode != 2000) {
            //请求错误
            return Promise.reject(res.data.status)
        }
        return res.data
    })
}

function req(config) {
    return doRequest(config).then(res => {
        return Promise.resolve(res)
    }).catch(err => {
        if (err.code == 40001) {
            wx.removeStorageSync('sid')
            return doRequest(config)
        }
        if (err.code == -1) {
            wx.showModal({
                title: '提示',
                content: '请您允许授权呢称和头像信息,并在接下来的提示中勾选用户信息并点击确定,谢谢！',
                showCancel: false,
                success: res => {
                    res.confirm && wx.openSetting({
                        success: s => {
                            if (s.authSetting['scope.userInfo']) {
                                return doRequest(config)
                            } else {
                                return Promise.reject('用户拒绝授权，重试')
                                console.log('0000')
                            }
                        }
                    })
                }
            })
            return Promise.reject('等待用户授权')
        } 

        err.message && getApp().wxToast.error(err.message)
        return Promise.reject(err)
    })
}
module.exports = {
    get: function (url, config = {}) {
        config.url = baseUrl + url
        return req(config)
    },
    post: function (url, config = {}) {
        config.url = baseUrl + url
        return req(config)
    }
}